<!DOCTYPE html>
<html>

	<head>
		<meta charset="UTF-8">
		<link rel="stylesheet" type="text/css" href="../../../CSS/default.css" />
		<link rel="stylesheet" type="text/css" href="../../../Themes/default/easyui.css">
		<link rel="stylesheet" type="text/css" href="../../../Themes/icon.css">
		<link rel="stylesheet" type="text/css" href="../../../CSS/demo.css">
		<link rel="stylesheet" type="text/css" href="../../../CSS/common.css">

	</head>

	<body style="background-color: white;padding: 0px;">
		<div id="toolbar" class="receipts_toolbar_style"></div>
		<form id="ff" method="post" style="margin-left: 15%;">
			<div style="display: none;">
				<input id="input_ID" class="easyui-textbox" type="text" name="ID" style="width:50%;"></input>
			</div>
			<div style="width: 90%;">
				<div style="margin-top: 20px;">
					<label>模板名称</label>
					<input id="input_NAME" class="easyui-textbox" type="text" name="NAME" style="width:50%;" data-options="required:true"></input>
				</div>
				<!--必须为这个div-->
				<div id="smsContent"></div>
			</div>
		</form>
	</body>
	<script type="text/javascript" src="../../../JS/jquery.min.js"></script>
	<script type="text/javascript" src="../../../JS/jquery.easyui.min.js"></script>
	<script type="text/javascript" src="../../../JS/common.js"></script>
	<script type="text/javascript" src="../../../JS/MarketShareFunction/SMSShareFunction.js"></script>
	<script type="text/javascript">
		var id = getID();
		var currid = AddFlag;
		var currPage = 0; //当前的页数
		var MaxPage = 0;
		var MinPage = 1;
		$(document).ready(function(){
		if(id != AddFlag) {
			currid = id;
			loadRemote();
		}
		});
        var content = IniSMSContent("CONTEXT");
		$("#smsContent").append(content);
		
		var receipts_type = RECEIPT_FRONT_SYMBOL;
		receipts_type += RECEIPT_NEXT_SYMBOL;
		receipts_type += RECEIPT_ADD_SYMBOL;
		receipts_type += RECEIPT_SAVE_SYMBOL;
		receipts_type += RECEIPT_DELETE_SYMBOL;
		content = AddReceiptsToolBar(receipts_type, "");
		$("#toolbar").append(content);
		getPageMaxAndMin();
		
		function save() {
			var isValid = $("#ff").form('validate');
			if(!isValid) {
				$.messager.alert("警告", "有必填项没有填写");
				return;
			}
			SaveData();
		}

		function getPageMaxAndMin() {
			var _url = RemoteLinkAddress + "/smstemplate/getmaxminid.do";
			$.ajax({
				type: "get",
				url: _url,
				dataType: "json",
				success: function(result) {
					if(result != null) {
						MaxPage = result.MAXID;
						MinPage = result.MINID;
						currPage = result.MINID + 1;
					}
				},
				error: function(xhr, status, error) {
					$.messager.alert("错误", "请求失败!");
				}
			});
		}

		function newdata() { //新建
			currPage = 0;
			$('#ff').form('reset');
			currid = AddFlag;
		}

		function frontpage() { //上一张
			if(currPage > MinPage) {
				var _url = RemoteLinkAddress + '/smstemplate/pre.do?ID=' + currPage;
				getRemoteData(_url);
			} else {
				$.messager.alert("提示", "没有上张");
			}
		}

		function nextpage() { //下一张
			if(currPage < MaxPage) {
				var _url = RemoteLinkAddress + '/smstemplate/next.do?ID=' + currPage;
				getRemoteData(_url);
			} else {
				$.messager.alert(" 提示", "没有下张");
			}
		}

		function loadRemote() {
			var _url=RemoteLinkAddress + '/smstemplate/findbyid.do?ID=' + currid;
			getRemoteData(_url);
		}

		function cls() {
			$('#ff').form('clear');
		}

		function deletedata() { //删除
			if(currid == AddFlag) {
				$.messager.alert("提示", "该单据为新建，不能删除！");
				return;
			}
			$.messager.confirm('Confirm', '是否删除', function(r) {
				if(r) {
					$.post(RemoteLinkAddress + "/smstemplate/delete.do", {
							ID: currid,
						},
						function(result) {
							if(result.msg == "success") {
								$.messager.alert({ // show success message
									title: '成功',
									msg: '删除成功'
								});
								nextpage();
							} else {
								$.messager.alert({ // show error message
									title: 'Error',
									msg: result.msg
								});
							}
						},
						'json');
				}
			});
		}

		function SaveData() {
			var _url = '';
			data = $('#ff').serialize();
			if(currid == AddFlag) {
				_url = RemoteLinkAddress + "/smstemplate/save.do";
				data = data.replace("&ID=", ""); //在新增保存的时候不需要传入ID值
			} else {
				_url = RemoteLinkAddress + "/smstemplate/update.do";
			}

			$.post(_url, data,
				function(result) {
					if(result.msg == "success") {
						var parentLocation;
						parentLocation = searchParent("SMSModuleList.html");
						parentLocation.dialogClose();
						$.messager.alert({ // show error message
							title: '成功',
							msg: '添加成功'
						});
					} else {
						$.messager.alert({ // show error message
							title: '错误',
							msg: "输入的数据已存在数据库"
						});
					}
				},
				'json');
		}
		
		function getRemoteData(_url) {
			$.ajax({
				type: "get",
				url: _url,
				dataType: "json",
				success: function(result) {
					if(result == null) {
						$.messager.alert("提示", "没有上张");
						return;
					}
					if(result != null) {
						currPage = result.ID;
						currid = result.ID;
						getFormData(result);
					}
				},
				error: function(xhr, status, error) {
					$.messager.alert("错误", "请求失败!");
				}
			});
		}
		
	</script>

</html>